from endOfPeriodProject.user.user import *
import sys


class UserManager(object):
    def __init__(self):
        self.user_list = []


    def run(self):
        self.load()
        # self.login()
        while True:
            try:
                self.question()
                answer = int(input("what do you want to do?"))
                if answer == 1:
                    self.add_new_user()
                elif answer == 2:
                    self.del_for_user()
                elif answer == 3:
                    self.upd_for_user()
                elif answer == 4:
                    self.query_all_user()
                elif answer == 5:
                    self.query_one_user()
                elif answer == 6:
                    self.save_user()
                elif answer == 7:
                    print("------Thank you for your ues!!!------")
                    break
                else:
                    print("unknown enter, please try again")
                    self.question()
                    self.run()
            except ValueError:
                print("unknown enter, please try again")
                self.question()
                self.run()

    def question(self):
        print("\033[34m 1.Insert a new user\033[0m\n"
              "\033[34m 2.Remove a user\033[0m\n"
              "\033[34m 3.Update a user's information\033[0m\n"
              "\033[34m 4.Query all user\033[0m\n"
              "\033[34m 5.Query one user\033[0m\n"
              "\033[34m 6.Save teachers information into user data\033[0m\n"
              "\033[34m 7.Edit\033[0m")

    def add_new_user(self):
        user_id = input("Enter your id: ")
        username = input("Enter your username: ")
        password = input("Enter your password: ")
        self.user_list.append(User(user_id, username, password))
        print("add success")

    def del_for_user(self):
        c = 0
        user_id = input("Enter the id what you want to delete: ")
        if len(self.user_list) == 0:
            print("List is null")
        else:
            for user in self.user_list:
                c += 1
                if user.id == user_id:
                    c -= 1
                    self.user_list.remove(user)
                    if c != self.user_list.__len__():
                        print("Not found")
            print("delete success")

    def upd_for_user(self):
        c = 0
        if len(self.user_list) == 0:
            print("List is null")
        else:
            user_id = input("Enter the id what you want to update: ")
            for user in self.user_list:
                c += 1
                if user.id == user_id:
                    user.id = input("Enter your id: ")
                    user.username = input("Enter your username: ")
                    user.password = input("Enter your password: ")
                    print(user)
                    if c == self.user_list.__len__():
                        print("Not found")

    def query_all_user(self):
        if len(self.user_list) == 0:
            print("List is null")
        else:
            for i in self.user_list:
                print(f"\033[33m{i}\033[0m")

    def query_one_user(self):
        c = 0
        if len(self.user_list) == 0:
            print("List is null")
        else:
            user_id = input("Please enter that user's name what you want query: ")
            for i in self.user_list:
                c += 1
                if i.id == user_id:
                    c -= 1
                    print(i)
                    if c == self.user_list.__len__():
                        print("Not find this user")

    def save_user(self):
        f = open("user.data", "w", encoding="utf-8")
        new_list = [i.__dict__ for i in self.user_list]
        a = str(new_list)
        f.write(a)
        f.close()

    def load(self):
        try:
            f = open("../user/user.data", "r", encoding="utf-8")
        except:
            f = open("../user/user.data", "w", encoding="utf-8")
        else:
            data = f.read()
            new_list = eval(data)
            self.user_list = [User(i["id"], i["username"], i["password"]) for i in new_list]
            f.close()

    def login(self):
        login_name = input("\033[34m Enter your login name: \033[0m")
        login_password = input("\033[34m Enter your password: \033[0m")
        for i in self.user_list:
            if login_name == i.username and login_password == i.password:
                print("\033[32m ---Welcome user \"" + i.username + "\" to use---\033[0m")
            else:
                print("\033[31m User Not Found\033[0m")
                sys.exit()

    def login_for_all(self):
        self.load()
        self.login()